//
//  TeamListController.m
//  easystudy
//
//  Created by kane on 16/8/30.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "TeamListController.h"
#import "TeamListCell.h"
#import "DoctorInfoController.h"
#import "MJRefresh.h"

@interface TeamListController ()<UITableViewDataSource,UITableViewDelegate,MJRefreshBaseViewDelegate>
{
    IBOutlet UITableView *dataTable;
    NSMutableArray *dataList;
    EmptyView *emptyView;
    MJRefreshHeaderView *header;
}


@end

@implementation TeamListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    dataList = [NSMutableArray arrayWithArray:self.doctorList];
    [dataTable reloadData];
    
    
    [self initRefreshUI];
}

- (void)initRefreshUI
{
    header = [MJRefreshHeaderView header];
    header.scrollView = dataTable;
    header.delegate = self;
    
    emptyView = [[EmptyView alloc] init];
    [self.view insertSubview:emptyView belowSubview:dataTable];
    emptyView.hidden = dataList.count > 0;
}

- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    NSDictionary *params = @{@"cid":self.cid};
    [WebService findDoctorList:params WithBlock:^(id result, NSError *error) {
        if (!error) {
            [dataList removeAllObjects];
            [dataList addObjectsFromArray:result[@"docList"]];
            [dataTable reloadData];
            emptyView.hidden = dataList.count > 0;
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        [header endRefreshing];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)backBtnPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableView DataSource && Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"TeamListCell";
    TeamListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"TeamListCell" bundle:nil] forCellReuseIdentifier:identifier];
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSDictionary *item = dataList[indexPath.row];
    [cell setupDoctorInfo:item];
    cell.doctorHospitalLbl.text = self.hospitalName;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *item = dataList[indexPath.row];
    DoctorInfoController *infoVC = [[DoctorInfoController alloc] init];
    infoVC.dataInfo = item;
    infoVC.hospitalName = self.hospitalName;
    [self.navigationController pushViewController:infoVC animated:YES];
}
@end
